草庐IT

GCD ③ dispatch_barrier

全部标签

multithreading - Swift dispatch_after throwing is not a prefix unary operator 错误

我有以下代码:importSpriteKitimportFoundationclassGameScene:SKScene{varoccupiedCoordinates:NSMutableArray=NSMutableArray()funcaddShape(){//...shape.position=CGPoint(x:actualX,y:actualY)self.occupiedCoordinates.addObject(NSValue(CGPoint:shape.position))lethalfDuration=random(min:CGFloat(0.5),max:CGFloat

ios - Swift 3 中的 GCD 差异

当我注意到Swift3改变了它的语法时,我正在研究GrandCentralDispatch。那么,是这样的吗:letqueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)dispatch_async(queue){()->Voidinletimg1=Downloader.downloadImageWithURL(imageURLs[0])dispatch_async(dispatch_get_main_queue(),{self.imageView1.image=img1})}和这个有什么不同吗?Dispat

swift - 快速关闭时 gcd 中的弱 self

apiFunc(user:User.currentUser,start:0,limit:Constants.numberOfItemInOnePage,success:{[weakself](friends)->Voidindispatch_async(dispatch_get_main_queue(),{[weakself]()->VoidinifletstrongSelf=self{strongSelf.friendList=friendsstrongSelf.loading=falsestrongSelf.tableView.reloadData()}})},failure:ni

ios - Swift:我可以加减 `dispatch_time_t` 变量吗?

我需要在iOS中使用Swift做一些时间计算。我必须使用dispatch_walltime。我希望这可以被视为公理化。在涉及时间数学的地方,我想我可能会得到“只需使用NSDate”的响应,但请相信它:我必须遵守dispatch_walltime。现在,很明显为什么有人会建议使用NSDate,因为当您使用NSTimeInterval和NSDate以及那些好东西时,它非常棒轻松制作自定义时间戳并比较它们并进行各种时间数学计算。但我必须使用dispatch_time_t,特别是像这样创建的dispatch_walltime://GetthetimeIntervalofnow.letnowIn

swift - dispatch_async 与 dispatch_sync 在获取数据中的对比。 swift

看了那么多并行和并发的帖子,我还是很迷惑什么才是正确的取数据方式。例如,在我的项目中,我有一个供用户获取数据的按钮。我的代码如下所示。vararray=[Int]()funcfetchData(){........response(objects:[object],error:NSError?){forobjectinobjects{array.append(object.number)//assumeobject.numberreturnanInt}//confusehere.ShouldIuseasyncherebecauseIamworryiftheuser//clickthef

swift - 如何使用 GCD DispatchWorkItem.notify 避免数据竞争?

在XCode8.3上使用Swift3.1,使用ThreadSanitizer运行以下代码会发现数据竞争(请参阅代码中的写入和读取注释):privatefuncincrementAsync(){letitem=DispatchWorkItem{[weakself]inguardletstrongSelf=selfelse{return}strongSelf.x+=1//DispatchWorkItemdone")}item.notify(queue:.main){[weakself]inguardletstrongSelf=selfelse{return}print(">\(strong

ios - ViewController 在等待分派(dispatch)信号量时作为弹出窗口

我在发出URLJSON请求时使用DispatchSemaphore进行等待,这种等待可能需要一段时间。为了克服这种情况,我决定制作一个新View,并在发出请求时将其显示为弹出窗口。为此,我使用了以下代码:functableView(_tableView:UITableView,didSelectRowAtindexPath:IndexPath){self.showPopUp()letsemaphore=DispatchSemaphore(value:0)self.api.requestMedicationsByReagent(method:1,ean:"",hash:medHash!,

swift - Swift GCD 覆盖中的类型不匹配

我想使用dispatchIOchannel从文件描述符中读取一些数据。创建channel后,下一步是调用read,声明如下:funcread(offset:off_t,length:Int,queue:DispatchQueue,ioHandler:@escaping(Bool,DispatchData?,Int32)->Void)length参数的文档说:Thenumberofbytestoreadfromthechannel.SpecifySIZE_MAXtocontinuereadingdatauntilanEOFisreached.看起来很简单。就我而言,我想这样做—一直读到E

swift - dispatch_queue_t! 有什么区别?和 dispatch_queue_t?

header显示dispatch_get_global_queue返回globalqueue或NULL。*@result*ReturnstherequestedglobalqueueorNULLiftherequestedglobalqueue*doesnotexist.*/@available(OSX10.6,*)@warn_unused_resultpublicfuncdispatch_get_global_queue(identifier:Int,_flags:UInt)->dispatch_queue_t!为什么返回值是dispatch_queue_t!而不是可选的dispat

ios - 在惰性变量中使用 dispatch_once 或静态变量

来自Swiftguide:Ifapropertymarkedwiththelazymodifierisaccessedbymultiplethreadssimultaneouslyandthepropertyhasnotyetbeeninitialized,thereisnoguaranteethatthepropertywillbeinitializedonlyonce.所以,据我所知,使用classSomeClass{lazyvarsomeVar:SomeOtherClass={returnSomeOtherClass()}()}不是确保使用SomeClass实例的每个人都使用与S